﻿Public Class frmFont
    Private _ModalResult As DialogResult
    Public Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub

    Public Function ShowEx() As DialogResult
        _ModalResult = Windows.Forms.DialogResult.None
        InitFrm()
        Me.ShowDialog()
        Return _ModalResult
    End Function
    Private Sub InitSample()
        txtSample.Text = "abcdefg123456" & vbNewLine & _
                       "あいうえお" & vbNewLine & _
                       "abcde@example.com" & vbNewLine & _
                       "http://www.example.com" & vbNewLine & _
                       "全角スペース：      " & vbNewLine & _
                       "半角スペース：　　　" & vbNewLine & _
                       "タブ：" & ControlChars.Tab & vbNewLine
    End Sub

    Private Sub InitFrm()
        RemoveEvents()
        InitCboFont()
        InitSample()
        InitSettings()
        AddEvents()
    End Sub
    Private Sub RemoveEvents()
        RemoveHandler chkMBSpace.CheckedChanged, AddressOf chkMBSpace_CheckedChanged
        RemoveHandler chkSpace.CheckedChanged, AddressOf chkSpace_CheckedChanged
        RemoveHandler chkTab.CheckedChanged, AddressOf chkTab_CheckedChanged
        RemoveHandler chkCrLf.CheckedChanged, AddressOf chkCrLf_CheckedChanged
        RemoveHandler chkSelectedRow.CheckedChanged, AddressOf chkSelectedRow_CheckedChanged
        RemoveHandler chkRowNumber.CheckedChanged, AddressOf chkRowNumber_CheckedChanged
        RemoveHandler chkEOF.CheckedChanged, AddressOf chkEof_CheckedChanged
        RemoveHandler chkRuler.CheckedChanged, AddressOf chkRuler_CheckedChanged
        RemoveHandler chkURI.CheckedChanged, AddressOf chkURI_CheckedChanged
        RemoveHandler cboFont.SelectedIndexChanged, AddressOf cboFont_SelectedIndexChanged
        RemoveHandler chkBold.CheckedChanged, AddressOf chkBold_CheckedChanged
        RemoveHandler chkItalic.CheckedChanged, AddressOf chkItalic_CheckedChanged
        RemoveHandler txtFontSize.ValueChanged, AddressOf txtFontSize_ValueChanged
        RemoveHandler txtTabWidth.ValueChanged, AddressOf txtTabWidth_ValueChanged
    End Sub
    Private Sub AddEvents()
        AddHandler chkMBSpace.CheckedChanged, AddressOf chkMBSpace_CheckedChanged
        AddHandler chkSpace.CheckedChanged, AddressOf chkSpace_CheckedChanged
        AddHandler chkTab.CheckedChanged, AddressOf chkTab_CheckedChanged
        AddHandler chkCrLf.CheckedChanged, AddressOf chkCrLf_CheckedChanged
        AddHandler chkSelectedRow.CheckedChanged, AddressOf chkSelectedRow_CheckedChanged
        AddHandler chkRowNumber.CheckedChanged, AddressOf chkRowNumber_CheckedChanged
        AddHandler chkEOF.CheckedChanged, AddressOf chkEof_CheckedChanged
        AddHandler chkRuler.CheckedChanged, AddressOf chkRuler_CheckedChanged
        AddHandler chkURI.CheckedChanged, AddressOf chkURI_CheckedChanged
        AddHandler cboFont.SelectedIndexChanged, AddressOf cboFont_SelectedIndexChanged
        AddHandler chkBold.CheckedChanged, AddressOf chkBold_CheckedChanged
        AddHandler chkItalic.CheckedChanged, AddressOf chkItalic_CheckedChanged
        AddHandler txtFontSize.ValueChanged, AddressOf txtFontSize_ValueChanged
        AddHandler txtTabWidth.ValueChanged, AddressOf txtTabWidth_ValueChanged
    End Sub
    Private ReadOnly Property Settings() As TEditorSettings
        Get
            Return frmMain.EditorSettings
        End Get
    End Property

    Private Sub InitSettings()
        cboFont.SelectedItem = Settings.FontName
        txtFontSize.Value = Settings.FontSize
        chkBold.Checked = Settings.Bold
        chkItalic.Checked = Settings.Italic

        txtTabWidth.Value = Settings.TabWidth

        cmdForeColor.BackColor = Settings.ForeColor
        cmdBackColor.BackColor = Settings.BackColor

        chkMBSpace.Checked = Settings.ShowMBSpace
        chkSpace.Checked = Settings.ShowSpace
        chkTab.Checked = Settings.ShowTab
        cmdSpace.BackColor = Settings.SpaceColor

        chkCrLf.Checked = Settings.ShowCrLf
        cmdCrLf.BackColor = Settings.CrLfColor

        chkSelectedRow.Checked = Settings.ShowSelectedRow
        cmdSelectedRow.BackColor = Settings.SelectedRowColor

        cmdEof.BackColor = Settings.EofColor
        cmdRowNumber.BackColor = Settings.RowNumberForeColor
        cmdRowNumberBack.BackColor = Settings.RowNumberBackColor

        chkEOF.Checked = Settings.ShowEof
        chkRowNumber.Checked = Settings.ShowRowNumber
        chkRuler.Checked = Settings.ShowRuler
        chkURI.Checked = Settings.ShowURI
        Settings.Apply(txtSample)
        txtSample.Refresh()
    End Sub

    Private Sub InitCboFont()
        cboFont.Items.Clear()
        cboFont.DrawMode = DrawMode.OwnerDrawFixed
        cboFont.ItemHeight = 20
        AddHandler cboFont.DrawItem, AddressOf cboFont_DrawItem
        For i As Integer = 0 To Settings.FontCount - 1
            cboFont.Items.Add(Settings.Font(i).Name)
        Next
    End Sub

    Private Sub cboFont_DrawItem(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DrawItemEventArgs)
        e.DrawBackground()
        Dim cmb As ComboBox = CType(sender, ComboBox)
        '項目に表示する文字列
        Dim txt As String
        If e.Index > -1 Then
            txt = cmb.Items(e.Index).ToString()
        Else
            txt = cmb.Text
        End If
        '使用するフォント
        Dim f As New Font(txt, cmb.Font.Size)
        '使用するブラシ
        Dim b = New SolidBrush(e.ForeColor)
        '文字列を描画する
        Dim ym As Single = _
            (e.Bounds.Height - e.Graphics.MeasureString(txt, f).Height) / 2
        e.Graphics.DrawString(txt, f, b, e.Bounds.X, e.Bounds.Y + ym)
        f.Dispose()
        b.Dispose()
        'フォーカスを示す四角形を描画
        e.DrawFocusRectangle()
    End Sub

    Private Sub chkMBSpace_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMBSpace.CheckedChanged
        txtSample.DrawsFullWidthSpace = chkMBSpace.Checked
        Settings.ShowMBSpace = chkMBSpace.Checked
        txtSample.Refresh()
    End Sub

    Private Sub chkSpace_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSpace.CheckedChanged
        txtSample.DrawsSpace = chkSpace.Checked
        Settings.ShowSpace = chkSpace.Checked
        txtSample.Refresh()
    End Sub

    Private Sub chkTab_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTab.CheckedChanged
        txtSample.DrawsTab = chkTab.Checked
        Settings.ShowTab = chkTab.Checked
        txtSample.Refresh()
    End Sub

    Private Sub chkCrLf_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCrLf.CheckedChanged
        txtSample.DrawsEolCode = chkCrLf.Checked
        Settings.ShowCrLf = chkCrLf.Checked
        txtSample.Refresh()
    End Sub

    Private Sub chkSelectedRow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSelectedRow.CheckedChanged
        txtSample.HighlightsCurrentLine = chkSelectedRow.Checked
        Settings.ShowSelectedRow = chkSelectedRow.Checked
        txtSample.Refresh()
    End Sub

    Private Sub chkRowNumber_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRowNumber.CheckedChanged
        txtSample.ShowsLineNumber = chkRowNumber.Checked
        Settings.ShowRowNumber = chkRowNumber.Checked
        txtSample.Refresh()
    End Sub

    Private Sub chkEof_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEOF.CheckedChanged
        txtSample.DrawsEofMark = chkEOF.Checked
        Settings.ShowEof = chkEOF.Checked
        txtSample.Refresh()
    End Sub

    Private Sub chkRuler_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRuler.CheckedChanged
        txtSample.ShowsHRuler = chkRuler.Checked
        Settings.ShowRuler = chkRuler.Checked
        txtSample.Refresh()
    End Sub

    Private Sub chkURI_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkURI.CheckedChanged
        txtSample.MarksUri = chkURI.Checked
        Settings.ShowURI = chkURI.Checked
        txtSample.Refresh()
    End Sub

    Private Sub cmdSpace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSpace.Click
        ColorDlg.Color = cmdSpace.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdSpace.BackColor = ColorDlg.Color
            txtSample.ColorScheme.WhiteSpaceColor = ColorDlg.Color
            Settings.SpaceColor = ColorDlg.Color
            txtSample.Refresh()
        End If
    End Sub

    Private Sub cmdCrLf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCrLf.Click
        ColorDlg.Color = cmdCrLf.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdCrLf.BackColor = ColorDlg.Color
            txtSample.ColorScheme.EolColor = ColorDlg.Color
            Settings.CrLfColor = ColorDlg.Color
            txtSample.Refresh()
        End If
    End Sub

    Private Sub cmdSelectedRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSelectedRow.Click
        ColorDlg.Color = cmdSelectedRow.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdSelectedRow.BackColor = ColorDlg.Color
            txtSample.ColorScheme.HighlightColor = ColorDlg.Color
            Settings.SelectedRowColor = ColorDlg.Color
            txtSample.Refresh()
        End If
    End Sub

    Private Sub cmdRowNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRowNumber.Click
        ColorDlg.Color = cmdRowNumber.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdRowNumber.BackColor = ColorDlg.Color
            txtSample.ColorScheme.LineNumberBack = ColorDlg.Color
            Settings.RowNumberBackColor = ColorDlg.Color
            txtSample.Refresh()
        End If
    End Sub

    Private Sub cmdEof_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEof.Click
        ColorDlg.Color = cmdEof.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdEof.BackColor = ColorDlg.Color
            txtSample.ColorScheme.EofColor = ColorDlg.Color
            Settings.EofColor = ColorDlg.Color
            txtSample.Refresh()
        End If

    End Sub

    Private Sub cmdRowNumberBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRowNumber.Click
        ColorDlg.Color = cmdRowNumberBack.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdRowNumberBack.BackColor = ColorDlg.Color
            txtSample.ColorScheme.LineNumberBack = ColorDlg.Color
            Settings.RowNumberBackColor = ColorDlg.Color
            txtSample.Refresh()
        End If
    End Sub

    Private Sub cmdForeColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdForeColor.Click
        ColorDlg.Color = cmdForeColor.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdForeColor.BackColor = ColorDlg.Color
            txtSample.ColorScheme.ForeColor = ColorDlg.Color
            Settings.ForeColor = ColorDlg.Color
            txtSample.Refresh()
        End If
    End Sub

    Private Sub cmdBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBackColor.Click
        ColorDlg.Color = cmdBackColor.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdBackColor.BackColor = ColorDlg.Color
            txtSample.ColorScheme.BackColor = ColorDlg.Color
            Settings.BackColor = ColorDlg.Color
            txtSample.Refresh()
        End If
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Settings.Save()
        _ModalResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Settings.Load()
        _ModalResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    Private Property FontSize() As Integer
        Get
            If txtFontSize.Value <= 0 Then
                Return 9
            Else
                Return txtFontSize.Value
            End If
        End Get
        Set(ByVal value As Integer)
            If value > 0 Then
                txtFontSize.Value = value
            End If
        End Set
    End Property
    Private Function GetFont() As Font
        Dim fs As FontStyle = FontStyle.Regular
        If chkBold.Checked Then
            fs = fs Or FontStyle.Bold
        End If
        If chkItalic.Checked Then
            fs = fs Or FontStyle.Italic
        End If
        Return New Font(cboFont.Text, Me.FontSize, fs)
    End Function

    Private Sub cboFont_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFont.SelectedIndexChanged
        txtSample.Font = GetFont()
        Settings.FontName = cboFont.Text
        txtSample.Refresh()
    End Sub

    Private Sub chkBold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBold.CheckedChanged
        txtSample.Font = GetFont()
        Settings.Bold = chkBold.Checked
        txtSample.Refresh()
    End Sub

    Private Sub chkItalic_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkItalic.CheckedChanged
        txtSample.Font = GetFont()
        Settings.Italic = chkItalic.Checked
        txtSample.Refresh()
    End Sub

    Private Sub txtFontSize_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFontSize.ValueChanged
        txtSample.Font = GetFont()
        Settings.FontSize = Me.FontSize
        txtSample.Refresh()
    End Sub

    Private Property TabWidth() As Integer
        Get
            If txtTabWidth.Value <= 0 Then
                Return 8
            Else
                Return txtTabWidth.Value
            End If
        End Get
        Set(ByVal value As Integer)
            If value > 0 Then
                txtTabWidth.Value = value
            End If
        End Set
    End Property

    Private Sub txtTabWidth_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTabWidth.ValueChanged
        txtSample.TabWidth = Me.TabWidth
        Settings.TabWidth = Me.TabWidth
        txtSample.Refresh()
    End Sub

End Class